﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExplorerPlus.AmazonApi;
using ExplorerPlus.DomainObjects;
using ExplorerPlus.Utilities;

namespace ExplorerPlus.Plugins.Common
{
    public static class PluginHelper
    {
        public static List<string> GetBrowseNodes(AmazonApi.AmazonWebService.BrowseNodes nodes, string filter)
        {
            List<string> catArr = new List<string>();
            for (int i = 0; i < nodes.BrowseNode.Length; i++)
            {
                AmazonApi.AmazonWebService.BrowseNode node = nodes.BrowseNode[i];
                string category = GetNodeValue(node, filter, node.Name);
                if (category != string.Empty)
                {
                    if (!catArr.Contains(category))
                    {
                        catArr.Add(category);
                    }
                }
            }
            return catArr;
        }

        public static List<string> GetBrowseNodesParent(AmazonApi.AmazonWebService.BrowseNodes nodes, string filter)
        {
            List<string> catArr = new List<string>();
            for (int i = 0; i < nodes.BrowseNode.Length; i++)
            {
                AmazonApi.AmazonWebService.BrowseNode node = nodes.BrowseNode[i];
                string category = GetNodeValueParent(node, filter, node.Name);
                if (category != string.Empty)
                {
                    if (!catArr.Contains(category))
                    {
                        catArr.Add(category);
                    }
                }
            }
            return catArr;
        }

        private static string GetNodeValue(AmazonApi.AmazonWebService.BrowseNode node, string filter, string childCategory)
        {
            if (node.Name == filter)
            {
                return childCategory;
            }
            else if (node.Ancestors != null)
            {
                return GetNodeValue(node.Ancestors[0], filter, childCategory);
            }
            else
            {
                return string.Empty;
            }
        }

        private static string GetNodeValueParent(AmazonApi.AmazonWebService.BrowseNode node, string filter, string childCategory)
        {
            if (node.Name == filter)
            {
                return childCategory;
            }
            else if (node.Ancestors != null)
            {
                return GetNodeValueParent(node.Ancestors[0], filter, node.Name);
            }
            else
            {
                return string.Empty;
            }
        }

        public static void SaveImageToDisk(Item item, int fieldId, string path)
        {
            // Add logic to download book images to images folder
            if (item.GetItemPropertyValue(fieldId).Length > 0)
            {
                string filePath = ExplorerPlus.Utilities.Web.SaveImageToDisk(
                    item.GetItemPropertyValue(fieldId), path);

                item.SetItemPropertyValue(fieldId, filePath);
            }
        }

        public static void OpenFile(string filePath)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            if (filePath != string.Empty || filePath.Trim().Length > 0)
            {
                if (System.IO.File.Exists(filePath))
                {
                    try
                    {
                        System.Diagnostics.Process.Start(filePath);
                    }
                    catch (System.ComponentModel.Win32Exception) { }
                }
            }

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        public static void OpenDirectory(string filePath)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            if (filePath != string.Empty || filePath.Trim().Length > 0)
            {
                if (System.IO.File.Exists(filePath) || System.IO.Directory.Exists(filePath))
                {
                    try
                    {
                        filePath = System.IO.Path.GetDirectoryName(filePath);
                        System.Diagnostics.Process.Start(filePath);
                    }
                    catch (System.ComponentModel.Win32Exception) { }
                }
            }

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        public static void VisitPage(string url)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            if (url != string.Empty || url.Trim().Length > 0)
            {
                System.Diagnostics.Process.Start(url);
            }

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        public static void ExtractValueList(Item item, int fieldId, string values)
        {
            // clear all old
            item.RemoveItemProperty(fieldId);
            // extract new values and add them to item
            if (values.IndexOf(',') > 0)
            {
                foreach (string value in values.Split(new char[] { ',' }))
                {
                    item.AddItemProperty(fieldId, value.Trim());
                }
            }
            else
            {
                item.AddItemProperty(fieldId, values);
            }
        }

    }
}
